<template>
  <div class="box">
    <div class="course-top">
      <div class="top-left">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>体育作业</el-breadcrumb-item>
          <el-breadcrumb-item>体育作业计划</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div @click="back()" class="top-right">返回</div>
    </div>
    <div class="container">
      <!-- <div class="top">
        <el-input
          v-model="model.jName"
          @change="getPlanList"
          style="width: 200px"
          placeholder="请输入作业计划名称"
          prefix-icon="el-icon-search"
        >
        </el-input>
        <div>
          <el-button size="small" @click="toAplan" type="primary"
            >布置作业</el-button
          >
        </div>
      </div> -->
      <el-table
        v-loading="isLoading"
        :data="tableData"
        element-loading-text="Loading"
        :header-cell-style="{
          background: '#FAFAFA',
          color: '#333333',
        }"
      >
        <!--      row-click 行点击事件-->
        <el-table-column
          align="center"
          prop="jName"
          label="作业计划名称"
        ></el-table-column>
        <el-table-column align="center" label="计划状态">
          <template slot-scope="scope">
            <el-switch
              @change="changeState(scope.row)"
              :active-value="2"
              :inactive-value="1"
              v-model="scope.row.jStatus"
              active-color="#13ce66"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="AI评价">
          <template slot-scope="scope">
            <el-switch
              @change="changeState(scope.row)"
              :active-value="1"
              :inactive-value="2"
              v-model="scope.row.jAutomatic"
              active-color="#13ce66"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              plain
              @click="lookMessage($event, scope.row)"
              >查看</el-button
            >
            <el-button
              style="margin-left: 15px"
              size="small"
              plain
              type="danger"
              @click="decrementMessage($event, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination-page :total="total" @size="changeSize" @page="changePage" />
    </div>
  </div>
</template>

<script>
import { PlanList, changePlanStatus, decrementPlan } from "@/api/sports/plan";
export default {
  name: "index",
  data() {
    return {
      key: "",
      tableData: [],
      value: true,
      model: {
        pageNum: 1,
        pageSize: 10,
        jName: "",
      },
      total: -1,
      isLoading: false,
    };
  },
  created() {
    this.getPlanList();
  },
  methods: {
    back() {
      this.$router.back();
    },
    xxxTableRowClassName({ row, rowIndex }) {
      this.dialogFormVisible = true;
      if (rowIndex % 2 !== 0) {
        return "statistics-warning-row";
      } else {
        return "";
      }
    },
    lookMessage(evt, e) {
      let target = evt.target;
      if (target.nodeName == "SPAN") {
        target = evt.target.parentNode;
      }
      target.blur();
      this.$router.push({
        path: "/plan/planNewAdd",
        query: {
          id: e.jId,
          type: 1,
        },
      });
    },
    decrementMessage(evt, e) {
      let target = evt.target;
      const { jId } = e;
      this.$confirm("此操作将永久删除该作业计划, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          decrementPlan({ jId, jeSource: 5 }).then((response) => {
            if (response.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getPlanList();
            } else {
              this.$message({
                type: "error",
                message: response.message,
              });
            }
          });
        })
        .catch(() => {
          if (target.nodeName == "SPAN") {
            target = evt.target.parentNode;
          }
          target.blur();
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    toAplan() {
      this.$router.push({
        path: "/sports/Aplan",
      });
    },
    changeSize(e) {
      this.model.pageSize = e;
      this.getPlanList();
    },
    changePage(e) {
      this.model.pageNum = e;
      this.getPlanList();
    },
    async getPlanList() {
      this.isLoading = true;
      const result = await PlanList({
        ...this.model,
        jShId: this.$store.state.user.USER_INFO.pscShId,
        uId: this.$store.state.user.USER_INFO.id,
      });
      if (result.code === 200) {
        this.tableData = result.result.list;
        this.total = result.result.total;
        this.isLoading = false;
      }
    },
    changeState(e) {
      this.isLoading = true;
      const { jId, jStatus, jAutomatic } = e;
      changePlanStatus({ jId, jStatus, jAutomatic }).then((response) => {
        if (response.code === 200) {
          this.isLoading = false;
          this.$message.success(response.message);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  padding: 2vh 15vw;
}
.course-top {
  display: flex;
  justify-content: space-between;
  padding: 1vh 0;
  ::v-deep .el-breadcrumb__inner {
    font-size: 1vw;
  }
  .top-right {
    font-size: 1vw;
    color: #1677ff;
  }
}
.container {
  height: 84vh;
  max-height: 84vh;
  overflow-y: auto;
  background: #fff;
}
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
}
::v-deep .el-table__row.statistics-warning-row {
  background: rgba(61, 157, 255, 0.05);
}
</style>
